package com.wx.frame.controller;

import com.alibaba.fastjson.JSON;
import com.wx.frame.model.Roles;
import com.wx.frame.model.RolesMenu;
import com.wx.frame.model.TreeView;
import com.wx.frame.service.RolesMenuService;
import com.wx.frame.service.RolesService;
import com.wx.utils.AjaxMessage;
import com.wx.utils.Page;
import com.wx.utils.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by myd on 15/12/25.
 */
@Controller("rolesController")
@RequestMapping("/roles")
public class RolesController {
    /**
     * 注入service
     */
    @Resource(name="rolesServiceImpl")
    public RolesService<Roles> rolesService;


    /**
     * 注入RolesMenuService
     */
    @Resource(name = "rolesMenuServiceImpl")
    public RolesMenuService<RolesMenu> rolesMenuService;
    /**
     * 获取用户列表
     */
    @RequestMapping(value = "/list",method ={RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Page<Roles> list(Pageable pageable, Roles roles)
    {
        return rolesService.findPage(pageable,roles);
    }

    /**
     * 保存用户信息
     * @param datas
     */
    @RequestMapping(value = "/save",method ={RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public AjaxMessage save(String datas){
        //System.out.println(datas);
        int result=0;
        List<Roles> roless = JSON.parseArray("["+datas+"]",Roles.class);
        for (Roles roles:roless){
            result = this.rolesService.save(roles);
        }
        String msg = "";
        if(1 == result) {
            msg = "保存成功！";
        } else {
            msg = "保存失败！！！";
        }
        return new AjaxMessage(1 == result ? true : false, msg);
    }

    /**
     * 删除用户信息，支持批量删除
     * @param ids
     */
    @RequestMapping(value = "/delete",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public AjaxMessage delete(String ids)
    {
        int result=0;
        String[] tmps =ids.split(",");
        if(null !=tmps && tmps.length>0){
            List<String> idList = new ArrayList<String>();
            for(String s: tmps) {
                idList.add(s);
            }
            result = this.rolesService.delete(idList);

        }
        String msg = "员工信息删除成功！";
        if(1 == result) {
            msg = "员工信息删除成功！";
        } else if(2 == result) {
            msg = "员工信息删除失败！！！";
        }

        return new AjaxMessage(1 == result ? true : false, msg);
    }


    /**
     * 保存用户信息
     * @param
     */
    @RequestMapping(value = "/saveRole",method ={RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public AjaxMessage saveRole(String roles_pk,String datas){
        System.out.println(roles_pk);
        System.out.println(datas);
        int result=0;

        //赋权之前删除之前角色的数据
        List<String> idList = new ArrayList<String>();
        idList.add(roles_pk);
        int res = rolesMenuService.delete(idList);
        if (res>=0) { //返回值为0是考虑到第一次给该角色赋权时可能不会删除任何数据
//            String[] strarray = menu_pk.split(",");
//            for (int i = 0; i < strarray.length; i++) {
//                RolesMenu rolesMenu = new RolesMenu();
//                rolesMenu.setFk_menu_pk(strarray[i]);
//                rolesMenu.setFk_roles_pk(roles_pk);
//                System.out.println(strarray[i]);
//                result = rolesMenuService.save(rolesMenu);
//            }
            List<TreeView> menus = JSON.parseArray(datas, TreeView.class);
            if (menus !=null && menus.size()>0)
            {
                for (TreeView menu : menus) {
                    RolesMenu rolesMenu = new RolesMenu();
                    rolesMenu.setFk_roles_pk(roles_pk);
                    rolesMenu.setFk_menu_pk(menu.getId());
                    rolesMenu.setChecked(menu.isChecked());
                    result = rolesMenuService.save(rolesMenu);
                }
            }
        }
        String msg = "";
        if(1 == result) {
            msg = "保存成功！";
        } else {
            msg = "保存失败！！！";
        }
        return new AjaxMessage(1 == result ? true : false, msg);
    }

}
